package com.mvvm.ktnet.application

import android.content.Context
import android.view.Gravity
import android.view.View
import com.drake.logcat.LogCat

import com.drake.tooltip.ToastConfig

import com.effective.android.anchors.Project
import com.effective.android.anchors.Task
import com.effective.android.anchors.TaskCreator
import com.kingja.loadsir.callback.SuccessCallback
import com.kingja.loadsir.core.LoadSir
import com.mvvm.ktnet.BuildConfig
import com.mvvm.ktnet.R
import com.mvvm.ktnet.http.base.RxHttpManager
import com.mvvm.ktnet.widget.loadCallBack.EmptyCallback
import com.mvvm.ktnet.widget.loadCallBack.ErrorCallback
import com.mvvm.ktnet.widget.loadCallBack.LoadingCallback
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.header.MaterialHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.tencent.mmkv.MMKV
import kotlinx.android.synthetic.main.layout_toast_error.view.*

/**
 *
 * @ProjectName:    JetPackViewModel
 * @Package:        com.android.me.alex.app
 * @ClassName:      AppTaskFactory
 * @Description:     java类作用描述
 * @Author:         Alex
 * @CreateDate:     2020/7/31 14:16
 * @UpdateUser:     更新者：
 * @UpdateDate:     2020/7/31 14:16
 * @UpdateRemark:   更新说明：
 * @Version:        1.0
 */
object TaskCreator : TaskCreator {
    override fun createTask(taskName: String): Task {
        return when (taskName) {
            InitNetWork.TASK_ID -> InitNetWork()
            InitMMKV.TASK_ID -> InitMMKV()
            InitUtils.TASK_ID -> InitUtils()
            InitLoading.TASK_ID -> InitLoading()
            else -> InitDefault()
        }
    }
}

class InitDefault : Task(TASK_ID, true) {
    companion object {
        const val TASK_ID = "0"
    }

    override fun run(name: String) {

    }
}

//初始化网络
class InitNetWork : Task(TASK_ID, true) {
    companion object {
        const val TASK_ID = "1"
    }

    override fun run(name: String) {


        RxHttpManager.init(App.context)



        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, _ ->
            ClassicsHeader(context)
        }
        SmartRefreshLayout.setDefaultRefreshFooterCreator { context, _ ->
            ClassicsFooter(context)
        }


    }
}

//初始化mmkv
class InitMMKV : Task(TASK_ID, true) {
    companion object {
        const val TASK_ID = "2"
    }

    override fun run(name: String) {
        MMKV.initialize(App.context.filesDir.absolutePath + "/mmkv")
    }
}

//初始化Utils
class InitUtils : Task(TASK_ID, true) {
    companion object {
        const val TASK_ID = "3"
    }

    override fun run(name: String) {
//        SuperXml.init(App.context)
        LogCat.config {
            defaultTag = "Kt_Net"
            enabled = BuildConfig.DEBUG
        }
    }
}

const val TOAST_ERROR = 0

//初始化Utils
class InitLoading : Task(TASK_ID, true) {
    companion object {
        const val TASK_ID = "4"

    }

    override fun run(name: String) {


        //界面加载管理 初始化
        LoadSir.beginBuilder()
            .addCallback(LoadingCallback())//加载
            .addCallback(ErrorCallback())//错误
            .addCallback(EmptyCallback())//空
            .setDefaultCallback(SuccessCallback::class.java)//设置默认加载状态页
            .commit()


        ToastConfig.apply {

            LogCat.e("shezhi toast")
            // 全局Toast
            onToast { context: Context, msg: CharSequence ->

                setGravity(Gravity.CENTER, 0, 0)
                view = View.inflate(context, R.layout.layout_toast_default, null)
                    .apply { tv_msg.text = msg }
            }
            // 等级Toast
            onLevel { context, msg, level ->
                when (level) {
                    TOAST_ERROR -> {
                        setGravity(Gravity.CENTER, 0, 0)
                        view = View.inflate(context, R.layout.layout_toast_error, null)
                            .apply { tv_msg.text = msg }
                    }
                }
            }
        }

    }
}


class AppTaskFactory : Project.TaskFactory(TaskCreator)